/**
 * 
 */
package com.google.code.jkippt.http.paging;

import java.util.Iterator;

import com.google.code.jkippt.parser.PageParser;
import com.google.code.jkippt.parser.ParserException;

/**
 * A result page.
 * @author Federico De Faveri defaveri@gmail.com
 */
public class Page<T> implements Iterable<T> {

	protected PageParser<T> parser;
	protected Meta meta;
	protected boolean consumed;
	
	/**
	 * @param parser
	 * @throws ParserException 
	 */
	public Page(PageParser<T> parser) throws ParserException {
		this.parser = parser;
		this.consumed = false;
		this.meta = parser.getMeta();
	}

	/**
	 * @return the meta
	 */
	public Meta getMeta() {
		return meta;
	}

	/**
	 * {@inheritDoc}
	 */
	public Iterator<T> iterator() {
		if (consumed) throw new IllegalStateException("Page iterator already consumed");
		consumed = true;
		return new PageItemIterator<T>(parser);
	}
}
